package com.sevencar.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.example.lightble.CmdSend;
import com.example.lightble.R;
import com.example.lightble.db.SettingDatabase;

import me.maxwin.view.SwitchButton;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;


public class GroupItem  extends LinearLayout{

	private Activity mActivity;
	private String  mGroupName;
	
	private TextView tvName;
	private Button btnDelete;
	private Button btnSetting;
	private CheckBox ckboxSelect;
	private LinearLayout  layoutOperaion;
	private ArrayList<String>  mDeviceList;
	private List<Map<String, String>>  mDeviceList_map = new ArrayList<Map<String,String>>();
	 private SwitchButton mBtnOpenButton;
	
	 private boolean isCheckedByUser=false;
	private onCheckedListener  mOnCheckedChangeListener;
	
	
	Button button_cbx;
	ProgressBar _progressBar;
	
	boolean isConnected = false;
	
	public interface onCheckedListener {
		void onCheckedChange(GroupItem view,boolean isChecked);  
	}
	public void setOnCheckedListener(onCheckedListener callback) {
		mOnCheckedChangeListener = callback;
	}
	
	//------------------
	private onDeleteBtnListener  mOnDeleteBtnListener;
	public interface onDeleteBtnListener {
		void onDelete(GroupItem view);  
	}
	public void setOnDeleteBtnListener(onDeleteBtnListener callback) {
		mOnDeleteBtnListener = callback;
	}
	public GroupItem(Activity acty,final String groupcode,String group_name) {
	        super(acty);
	        mActivity=acty;
	        mGroupName=group_name;
	        LayoutInflater inflater=(LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        inflater.inflate(R.layout._group_item,this);
	        
	        tvName=(TextView)findViewById(R.id.id_light_group_item_devname);
	        btnDelete=(Button)findViewById(R.id.id_light_group_item_deletebtn);
	        btnSetting=(Button)findViewById(R.id.id_light_group_item_setting);
//	        ckboxSelect=(CheckBox)findViewById(R.id.id_light_group_item_checkbox);
	        layoutOperaion=(LinearLayout)findViewById(R.id.id_light_group_item_layout_operation);
	        final LinearLayout ll_children=(LinearLayout)findViewById(R.id.ll_children);
	        ll_children.setVisibility(View.GONE);
	        
	        button_cbx = (Button) findViewById(R.id.id_lighctrl_item_cbx);
	           _progressBar  = (ProgressBar) findViewById(R.id.id_light_dev_process);
	        
	        button_cbx.setOnClickListener(new OnClickListener() {
	        				
	        				@Override
	        				public void onClick(View v) {
	        							if(isConnected)
	        							{
	        								setDisconnect(true);
	        							}else{
	        								_progressBar.setVisibility(View.VISIBLE);
	        								button_cbx.setVisibility(View.GONE);
	        								
	        								for (int i = 0; i < mDeviceList.size(); i++) {
	        									HomeFragment.mBLEServiceInstance.startConnectByUser(mDeviceList.get(i));
											}
	        							}  
	        				}
	        });
	        		
	        SettingDatabase dataBase= new SettingDatabase(mActivity, 0);
			mDeviceList_map =dataBase.query_maplist("select  device_address , device_name from table_group where group_code='"+groupcode+"' ");
			dataBase.close();
			
			mDeviceList = new ArrayList<String>();
			
			

//			Toast.makeText(mActivity, "---mDeviceList_map--"+mDeviceList_map.size(), 1000).show();
//			Toast.makeText(mActivity, "---HomeFragment.lstDevices.size()--"+HomeFragment.lstDevices.size(), 1000).show();
			for (int i = 0; i < mDeviceList_map.size(); i++) {
//				TextView tvv = new TextView(mActivity);
//				tvv.setText(mDeviceList_map.get(i).get("device_address"));
				
//				for (int j = 0; j < HomeFragment.lstDevices.size(); j++) {
//					Toast.makeText(mActivity, "----0---"+j, 1000).show();
//					if(mDeviceList_map.get(i).get("device_address").equals(HomeFragment.lstDevices.get(j).getDevAddress())){
//						ll_children.addView(HomeFragment.lstDevices.get(j));
//						Toast.makeText(mActivity, "-1------"+j, 1000).show();
//					}
//				}
				LightDevItem item= new LightDevItem(mActivity,mDeviceList_map.get(i).get("device_address").toString(),mDeviceList_map.get(i).get("device_name").toString());
				ll_children.addView(item);
				
				HomeFragment.lstDevices.add(item);
				
				mDeviceList.add(mDeviceList_map.get(i).get("device_address"));
			}
	        
	        layoutOperaion.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					
					if(ll_children.getVisibility() != GONE){
						ll_children.setVisibility(View.GONE);
					}else {
						ll_children.setVisibility(View.VISIBLE);
					}
				}
			});
	        
	        btnDelete.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
//					if(mOnDeleteBtnListener!=null)
//						mOnDeleteBtnListener.onDelete(GroupItem.this);
					
//					SettingDatabase.getInstance().deleteGroupNamelistItem(mGroupName);
//					SettingDatabase.getInstance().deleteGroupTable(mGroupName);
				}
			});
//	        btnSetting.setOnClickListener(new OnClickListener() {
//				
//				@Override
//				public void onClick(View v) {
////					showAddActivity();
//				}
//			});
//	        ckboxSelect.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//				
//				@Override
//				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//					
//					if(isCheckedByUser){
//						isCheckedByUser=false;
//						return ;
//					}
//					if(mOnCheckedChangeListener!=null){
//						 mOnCheckedChangeListener.onCheckedChange(GroupItem.this, isChecked);
//					 }
////					 if(isChecked){
////						 layoutOperaion.setVisibility(View.VISIBLE);
////					 }else{
////						 layoutOperaion.setVisibility(View.GONE);
////					 }
//				}
//			});
	        
	        mBtnOpenButton=(SwitchButton)findViewById(R.id.id_light_group_item_switchbtn);
	        mBtnOpenButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
				
				@Override
				public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//					Toast.makeText(mActivity, "功能", Toast.LENGTH_SHORT).show();
			    	byte[] test=CmdSend.getInstance().getOpenLightArray(isChecked);
			       	HomeFragment.mBLEServiceInstance.sendData(test,mDeviceList);
				}
			});
	        
	        tvName.setText(group_name);
	 }
	 public void onResume()
	 {
		  SettingDatabase sdb = new SettingDatabase(mActivity,0);
		  
		  mDeviceList=sdb.getDeviceFromGroupTable(mGroupName);
		  sdb.close();
	 }
	 public void cancelChecked()
	 {
		if(ckboxSelect.isChecked())
		{
			 isCheckedByUser=true;
			 ckboxSelect.setChecked(false);
			 layoutOperaion.setVisibility(View.GONE);
		}
	 }
	 private void showAddActivity()
	 {
//		 Intent intent=new Intent(mActivity,LightGroupAddActivity.class);
		 
//		 intent.putExtra("GroupName", mGroupName);
//		 mActivity.startActivity(intent);
	 }
	 
	 
	    /**
	     * 设备重连失败.
	     */
	    private void setReconnectFailed()
	    {
	    	Log.i("sno","重重重重重重重重重重重重重重重重重重重重重重重重重重重重重重重重");
	    	_progressBar.setVisibility(View.VISIBLE);
			button_cbx.setVisibility(View.GONE);
//			DeviceFragment.mBLEServiceInstance.startConnectByUser(mDevice.getAddress());
	    }
	    public void setConnecting()
	    {
	    	_progressBar.setVisibility(View.VISIBLE);
			button_cbx.setVisibility(View.GONE);
	    }
	    @SuppressLint("NewApi")
		public void setDisconnect(boolean byUser)
	    {
	    	_progressBar.setVisibility(View.GONE);
			button_cbx.setVisibility(View.VISIBLE);
			button_cbx.setBackground(getResources().getDrawable(R.drawable.checkbox_off));
//			setHintText("");
			mBtnOpenButton.setChecked(false);
			mBtnOpenButton.setEnabled(false);
			isConnected=false;
			if(byUser){
				for (int i = 0; i < mDeviceList.size(); i++) {
					HomeFragment.mBLEServiceInstance.setDisconnectByUser(mDeviceList.get(i));
				}
			}
	    }
}
